package com.ilearn.iodata.iolingo.config;

import com.ilearn.iodata.iolingo.service.processor.GenericPrimitiveProcessor;
import com.ilearn.iodata.iolingo.service.provider.GenericEdmProvider;
import org.apache.olingo.commons.api.edmx.EdmxReference;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataHttpHandler;
import org.apache.olingo.server.api.ServiceMetadata;
import org.apache.olingo.server.api.processor.EntityCollectionProcessor;
import org.apache.olingo.server.api.processor.EntityProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

@Configuration
@ConditionalOnBean(GenericEdmProvider.class)
public class OdataConfig {
    public OData odata;
    public ServiceMetadata serviceMetadata;

    @Autowired
    private EntityCollectionProcessor enityCollectionProcessor;

    @Autowired
    private EntityProcessor entityProcessor;

    @Autowired
    private GenericPrimitiveProcessor genericPrimitiveProcessor;

    @Autowired
    GenericEdmProvider genericEdmProvider;

    @Bean
    public ODataHttpHandler getHandler() {
        this.odata = OData.newInstance();
        this.serviceMetadata = odata.createServiceMetadata(genericEdmProvider,
                new ArrayList<EdmxReference>());
        ODataHttpHandler handler = odata.createHandler(this.serviceMetadata);
        handler.register(enityCollectionProcessor);
        handler.register(entityProcessor);
        handler.register(genericPrimitiveProcessor);
        return handler;
    }

}
